home *** CD-ROM | disk | FTP | other *** search
/ Almathera Ten Pack 3: CDPD 3 / Almathera Ten on Ten - Disc 3: CDPD3.iso / scope / 001-025 / scopedisk16 / ispell / amiga.c next >
C/C++ Source or Header  |  1995-03-18  |  8KB  |  424 lines

  1. /* -*- Mode:Text -*- */
  2. /*
  3.  * Deal w/ amiga console screen stuff.
  4.  *
  5.  *                     -- luis soltero, 5/12/88 --
  6.  *
  7.  *
  8.  */
  9. #include <stdio.h>
  10. #include <fcntl.h>
  11.  
  12. /*------------------------------------------------------------------------
  13.  * amiga console suport routines
  14.  */
  15.  
  16. #include <exec/types.h>
  17. #include <exec/exec.h>
  18. #include <intuition/intuition.h>
  19. #include <graphics/gfx.h>
  20. #include <libraries/dos.h>
  21. #include <libraries/dosextens.h>
  22.  
  23. /* console colors */
  24. #define WHITE_ON_BLACK    0
  25. #define WHITE_ON_BLUE    1
  26. #define BLACK_ON_WHITE    2
  27. #define BLUE_ON_WHITE    3
  28. #define ORANGE_ON_BLUE    4
  29.  
  30. #define SETCOLORS(k,x)    (setcolors(k, colors[x].style, colors[x].frw, colors[x].bak))
  31.  
  32. struct colortype {
  33.     char *style;
  34.     char *frw;
  35.     char *bak;
  36. } colors[5] = {
  37.     {"0", "31", "42"},        /* white on black */
  38.     {"0", "31", "40"},        /* white on blue  */
  39.     {"0", "32", "41"},        /* black on white */
  40.     {"0", "30", "41"},        /* blue on white  */
  41.     {"0", "33", "40"}        /* orange_on_blue */
  42. };
  43.  
  44. insert_line(rq)
  45. struct IOStdReq *rq;
  46. {
  47.      printc(rq,"%c%c",0x9b, 0x4c);
  48. }
  49.  
  50. scroll_up(rq, nl)
  51. struct IOStdReq *rq;
  52. int nl;
  53. {
  54.     char buf[10];
  55.     sprintf(buf,"%d",(short)nl);
  56.     printc(rq,"%c%s%c",0x9b, buf, 0x53);
  57. }
  58.  
  59. scroll_down(rq, nl)
  60. struct IOStdReq *rq;
  61. int nl;
  62. {
  63.     char buf[10];
  64.     sprintf(buf,"%d",(short)nl);
  65.     printc(rq,"%c%s%c",0x9b, buf, 0x54);
  66. }
  67.  
  68. cursor_off(rq)
  69. struct IOStdReq *rq;
  70. {
  71.      printc(rq,"%c%c%c%c",0x9b,0x30,0x20,0x70);
  72. }
  73.  
  74. cursor_on(rq)
  75. struct IOStdReq *rq;
  76. {
  77.     printf(rq,"%c%c%c",0x9b,0x20,0x70);
  78. }
  79.  
  80. beep(rq)
  81. struct IOStdReq *rq;
  82. {
  83.     printc(rq,"%c",0x7);
  84. }
  85.  
  86. con_set_line(rq,offset,len)
  87. struct IOStdReq *rq;
  88. int offset,        /* in raster col, dist from left of win */
  89.     len;        /* number of chars                */
  90. {
  91.     char buf[10];
  92.     sprintf(buf,"%d",(short)offset);
  93.     printc(rq,"%c%s%c",0x9b,buf,0x78);
  94.  
  95.     sprintf(buf,"%d",(short)len);
  96.     printc(rq,"%c%s%c",0x9b,buf,0x75);
  97. }
  98.  
  99. con_set_pagelen(rq)
  100. struct IOStdReq *rq;
  101. {
  102.     printc(rq,"%c%c",0x9b,0x74);
  103. }
  104.  
  105. Open_Console(rq,win)
  106.   struct IOStdReq     *rq;
  107.   struct Window        *win;
  108. {
  109.     rq->io_Data = (APTR) win;
  110.     rq->io_Length = sizeof(*win);
  111.     if (OpenDevice("console.device",0,rq,0))
  112.       return(FALSE);    /* could not get device */      
  113.     return(TRUE);    /* device opened successfully */
  114. }
  115.  
  116. GotoXY(rq,x,y)
  117.   struct IOStdReq    *rq;
  118.   ULONG            x,y;
  119. {
  120.     char buf[10];
  121.     sprintf(buf,"%c%d%c%d%c",0x9b,y,0x3b,x,0x48);
  122.     putcon(rq,buf);
  123. }
  124.  
  125. ClrScr(rq)
  126.   struct IOStdReq    *rq;
  127. {
  128.     char buf[10];
  129.     sprintf(buf,"%c",0x0c);
  130.     putcon(rq,buf);
  131. }
  132.  
  133. setcolors(rq,style,fg,bg)
  134. struct IOStdReq *rq;
  135. char *style, *fg, *bg;
  136. {
  137.     char buf[10];
  138.     sprintf(buf,"%c%s%c%s%c%s%c",0x9b,style,0x3b,fg,0x3b,bg,0x6d);
  139.     putcon(rq,buf);
  140. }
  141.  
  142. putcon(rq,str)
  143. struct IOStdReq *rq;
  144. char *str;
  145. {
  146.     /* in case the console has not been initialized */
  147.     if ( rq == NULL ) {
  148.         fprintf(stderr,"%s",str);
  149.     return;
  150.     }
  151.     rq->io_Command = CMD_WRITE;
  152.     rq->io_Data = (APTR) str;
  153.     rq->io_Length = -1;    
  154.     DoIO(rq);    
  155. }
  156.  
  157. printc(rq,fmt,a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12)
  158. struct  IOStdReq    *rq;
  159. ULONG      fmt,a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12;
  160. {
  161.     char    pbuff[256];
  162.     sprintf(pbuff,fmt,a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12);
  163.     putcon(rq,pbuff);
  164. }
  165.  
  166. struct NewWindow NewWin = {
  167.     0, 0,        /* left, top edge    */
  168.     0, 0,        /* width, hight      */
  169.     0, 1,        /* detail, block pen */
  170.     CLOSEWINDOW   | NEWSIZE,      /* IDCMP flags */
  171.     SMART_REFRESH | ACTIVATE    | /* flags       */
  172.     WINDOWCLOSE   | WINDOWDRAG  | 
  173.     WINDOWSIZING  | WINDOWDEPTH | 
  174.     GIMMEZEROZERO,
  175.     NULL, NULL,        /* gadget, check mark    */
  176.     NULL,        /* title             */
  177.     NULL, NULL,        /* screen, bitmap        */
  178.     200, 15,        /* min width, min height */
  179.     640, 200,        /* max width, max height */
  180.     WBENCHSCREEN    /* type             */
  181. };
  182.  
  183. Make_Window(w_ptr, title, x0, y0, x1, y1)
  184.   struct    Window    **w_ptr;
  185.   UBYTE        *title;
  186.   int        x0,y0,x1,y1;    /* upper left & lower right corners */
  187.  
  188. {
  189.     struct  Window         *OpenWindow();
  190.     
  191.     /* define the window in Amiga Intuition terms */
  192.  
  193.     NewWin.LeftEdge = x0;
  194.     NewWin.TopEdge = y0;
  195.     NewWin.Width = (x1 - x0);
  196.     NewWin.Height = (y1 - y0);
  197.     NewWin.Title = title;
  198.  
  199.     if ((*w_ptr = OpenWindow(&NewWin)) == NULL)
  200.       return(FALSE);    /* problem... return false */
  201.  
  202.     return(TRUE);    /* window opened okay... return true */
  203.  
  204. }    /* Make_Window() */
  205.  
  206. struct IntuitionBase *IntuitionBase;
  207.  
  208. /* open a 25x80 nondescript win to scroll text in */
  209. init_win(ApplWin, applname, ApplIOrq)
  210. struct Window   **ApplWin;
  211. char *applname;
  212. struct IOStdReq *ApplIOrq;
  213. {
  214.     struct IntuitionBase *OpenLibrary();
  215.         struct GfxBase *gfb;
  216.  
  217.     IntuitionBase = OpenLibrary("intuition.library",LIBRARY_VERSION);
  218.     if (IntuitionBase == NULL)
  219.       exit(FALSE);
  220.  
  221.     gfb = (struct GfxBase *)OpenLibrary("graphics.library",LIBRARY_VERSION);
  222.     if (gfb == NULL)
  223.       exit(FALSE);
  224.  
  225.     if (!(Make_Window(ApplWin,applname,0L,0L,640L,200L)))
  226.       exit (FALSE);
  227.       
  228.     ModifyIDCMP((*ApplWin), CLOSEWINDOW | NEWSIZE | ACTIVEWINDOW | 
  229.                  VANILLAKEY  | MENUPICK);
  230.     
  231.     if (! Open_Console(ApplIOrq,(*ApplWin) )){
  232.        CloseWindow((*ApplWin));
  233.        exit(FALSE);
  234.      }
  235.         con_set_line(ApplIOrq,0,130);
  236.     SETCOLORS(ApplIOrq,WHITE_ON_BLACK);
  237. }
  238.  
  239. /* terminate win opened by init_win() */
  240. close_win(ApplWin, ApplIOrq)
  241. struct Window   *ApplWin;
  242. struct IOStdReq *ApplIOrq;
  243. {
  244.     CloseWindow(ApplWin);
  245.     CloseDevice(ApplIOrq);
  246. }
  247.  
  248. /* get input from user and tokenize it */ 
  249. getevent(ApplWin) 
  250. struct Window *ApplWin;
  251. {
  252.     struct IntuiMessage *mess, *GetMsg();
  253.     int retval = -1;
  254.  
  255.     /* wait for an intuition event */
  256.     while( !(mess = GetMsg(ApplWin->UserPort)) )
  257.         Wait(1 << ApplWin->UserPort->mp_SigBit);
  258.  
  259.     switch(mess->Class) {
  260.     case CLOSEWINDOW:
  261.         retval = 'q';
  262.         break;
  263.     case VANILLAKEY: 
  264.         retval = mess->Code;
  265.         break;
  266.     case NEWSIZE : 
  267.         retval = 0x0c;
  268.         break;
  269.     case MENUPICK:
  270.     case ACTIVEWINDOW:
  271.     default:
  272.         break;
  273.     }
  274.     ReplyMsg(mess);
  275.     return(retval);
  276. }
  277.  
  278. /*
  279.  * end of amiga console support.
  280.  *-----------------------------------------------------------------------*/
  281.  
  282.  
  283. /*-------------------------------------------------------------------------
  284.  * terminal handling entry points
  285.  */
  286.  
  287. #include <sgtty.h>
  288. #include <signal.h>
  289. #include "ispell.h"
  290.  
  291. struct Window *scr_w = NULL;
  292. struct IOStdReq *scr_rq = NULL, _scr_rq;
  293.  
  294. printcon(fmt,a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12)
  295. ULONG      fmt,a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12;
  296. {
  297.     char    pbuff[256];
  298.     sprintf(pbuff,fmt,a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12);
  299.     putcon(scr_rq, pbuff);
  300. }
  301.  
  302. putccon(ch)
  303. char ch;
  304. {
  305.     printc(scr_rq,"%c", ch);
  306. }
  307.  
  308. getccon()
  309. {
  310.     int ch;
  311.  
  312.     while ( (ch=getevent(scr_w)) == -1 )
  313.         ;
  314.     if ( ch == '\b' || ch == 0x7f )
  315.         erasechar = ch;
  316.     return( ch );
  317. }
  318.  
  319. /* open a console window for spell */
  320. terminit ()
  321. {
  322.     erasechar = '\b';
  323.     scr_rq = &_scr_rq;
  324.     init_win(&scr_w, "ISpell", scr_rq);
  325. }
  326.  
  327. done ()
  328. {
  329.     unlink(tempfile);
  330.     close_win(scr_w, scr_rq);
  331.     exit(0);
  332. }
  333.  
  334. /* clear the screen */
  335. erase ()
  336. {
  337.     ClrScr(scr_rq);
  338. }
  339.  
  340. /* move to row, col */
  341. move (row, col)
  342. {
  343.     GotoXY(scr_rq, col, row);
  344. }
  345.  
  346. /* do stand out mode */
  347. inverse ()
  348. {
  349.     SETCOLORS(scr_rq, ORANGE_ON_BLUE); 
  350. }
  351.  
  352. /* do stand end mode */
  353. normal ()
  354. {
  355.     SETCOLORS(scr_rq, WHITE_ON_BLACK);
  356. }
  357.  
  358. /* do a back space */
  359. backup ()
  360. {
  361.     putch((int)'\b');
  362. }
  363.  
  364. putch (c)
  365. {
  366.     printc (scr_rq,"%c", c);
  367. }
  368.  
  369. /* not implemented on the amiga */
  370. onstop(signo) {}
  371. stop () {}
  372.  
  373. #define CLI    "NewCli"
  374. #define CLINAME "\"con:20/30/600/100/ISpell Cli\""
  375.  
  376. shellescape (buf)
  377. char *buf;
  378. {
  379.     fexecl(CLI,CLI,CLINAME,0);
  380. }
  381.  
  382. /*
  383.  * end of terminal control entry points
  384.  *-----------------------------------------------------------------------*/
  385.  
  386. /*-----------------------------------------------------------------------
  387.  * misc missing and broken stuff.
  388.  *
  389.  */
  390.  
  391. /* the manx access routine is broken, here is a brain damaged one
  392.    that works for this applications. */
  393. access(name)
  394. char *name;
  395. {
  396.     int in = open(name, O_RDWR);
  397.     if ( in == -1 ) 
  398.         return( -1 );
  399.     close (in);
  400.     return(0);
  401. }
  402.  
  403. /* i really do not understant why sleep is used in i spell. Here is
  404.    a null function used soley to keep ispell happy. */
  405. sleep(n) int n; {}
  406.  
  407. link(from, to)
  408. char *from, *to;
  409. {
  410.     FILE *f, *t;
  411.     int  ch;
  412.     if ( (f=fopen(from,"r")) == NULL )
  413.         return(-1);
  414.     if ( (t=fopen(to,"w")) == NULL ) {
  415.         fclose(f);
  416.     return(-1);
  417.     }
  418.     while((ch=getc(f)) != EOF )
  419.     putc(ch, t);
  420.     fclose(f);
  421.     fclose(t);
  422. }
  423.  
  424.